package com.cp.core.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cp.core.enums.BorrowAuthEnum;
import com.cp.core.enums.BorrowerStatusEnum;
import com.cp.core.enums.IntegralEnum;
import com.cp.core.mapper.BorrowerMapper;
import com.cp.core.pojo.entity.Borrower;
import com.cp.core.pojo.entity.BorrowerAttach;
import com.cp.core.pojo.entity.UserInfo;
import com.cp.core.pojo.entity.UserIntegral;
import com.cp.core.pojo.vo.BorrowerApprovalVO;
import com.cp.core.pojo.vo.BorrowerAttachVO;
import com.cp.core.pojo.vo.BorrowerDetailVO;
import com.cp.core.pojo.vo.BorrowerVo;
import com.cp.core.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author ming
 * @since 2022-07-27
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private BorrowerAttachService borrowerAttachService;
    @Resource
    private DictService dictService;
    @Resource
    private UserIntegralService userIntegralService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBorrowerVoByUserId(BorrowerVo borrowerVo, Long userId) {
        //保存借款人信息
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVo,borrower);
        borrower.setUserId(userId);
        UserInfo userInfo = userInfoService.getById(userId);
        borrower.setName(userInfo.getName());
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setMobile(userInfo.getMobile());
        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        this.baseMapper.insert(borrower);
        //保存附件
        List<BorrowerAttach> borrowerAttachList = borrowerVo.getBorrowerAttachList();
        for (BorrowerAttach borrowerAttach : borrowerAttachList) {
            borrowerAttach.setBorrowerId(borrower.getId());
        }
        borrowerAttachService.saveBatch(borrowerAttachList);
        //更新用户userInfo中借款人的转态
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        userInfoService.updateById(userInfo);

    }

    @Override
    public Integer getStatusByUserId(Long userId) {
        QueryWrapper<Borrower> user_id = new QueryWrapper<Borrower>()
                .eq("user_id", userId).select("status");
        List<Object> objects = this.baseMapper.selectObjs(user_id);
        if(objects.size()==0){
            return BorrowerStatusEnum.NO_AUTH.getStatus();
        }
        return (Integer)objects.get(0);
    }

    @Override
    public IPage<Borrower> listPage(Page<Borrower> pageParam, String keyWord) {
        if(StringUtil.isBlank(keyWord)){
            return this.baseMapper.selectPage(pageParam,null);
        }
        QueryWrapper<Borrower> wrapper = new QueryWrapper<>();
        wrapper.like("name",keyWord).or()
                .like("id_card",keyWord).or()
                .like("id",keyWord)
                .orderByDesc("create_time");
        return baseMapper.selectPage(pageParam,wrapper);
    }

    @Override
    public BorrowerDetailVO getBorrowerDetailVoById(Long id) {
        Borrower borrower = this.getById(id);
        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        BeanUtils.copyProperties(borrower,borrowerDetailVO);

        borrowerDetailVO.setMarry(borrower.getMarry()?"是":"否");
        borrowerDetailVO.setSex(borrower.getSex()==1?"男":"女");
        borrowerDetailVO.setStatus(BorrowerStatusEnum.getMsgByStatus(borrower.getStatus()));
        borrowerDetailVO.setIndustry(dictService.getNameByDictCodeAndValue("industry",borrower.getIndustry()));
        borrowerDetailVO.setEducation(dictService.getNameByDictCodeAndValue("education",borrower.getEducation()));
        borrowerDetailVO.setIncome(dictService.getNameByDictCodeAndValue("income",borrower.getIncome()));
        borrowerDetailVO.setReturnSource(dictService.getNameByDictCodeAndValue("returnSource",borrower.getReturnSource()));
        borrowerDetailVO.setContactsRelation(dictService.getNameByDictCodeAndValue("relation",borrower.getContactsRelation()));

        //附件列表
        List<BorrowerAttachVO> borrowerAttachVOList = borrowerAttachService.getByBorrow(borrower.getId());
        borrowerDetailVO.setBorrowerAttachVOList(borrowerAttachVOList);
        return borrowerDetailVO;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approval(BorrowerApprovalVO borrowerApprovalVO) {
        Borrower borrower = this.baseMapper.selectById(borrowerApprovalVO.getBorrowerId());
        //设置审核状态
        borrower.setStatus(borrowerApprovalVO.getStatus());
        this.baseMapper.updateById(borrower);
        //计算积分
        UserInfo userInfo = userInfoService.getById(borrower.getUserId());
        Integer integral  = userInfo.getIntegral();
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(borrower.getUserId());
        userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());
        userIntegral.setContent("借款人基本信息");
        userIntegralService.save(userIntegral);
        integral+=borrowerApprovalVO.getInfoIntegral();
        if (borrowerApprovalVO.getIsIdCardOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setUserId(borrower.getUserId());
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            userIntegral.setContent("借款人身份证信息");
            userIntegralService.save(userIntegral);
            integral+=IntegralEnum.BORROWER_IDCARD.getIntegral();
        }
        if (borrowerApprovalVO.getIsHouseOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setUserId(borrower.getUserId());
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            userIntegral.setContent("借款人房产信息");
            userIntegralService.save(userIntegral);
            integral+=IntegralEnum.BORROWER_HOUSE.getIntegral();
        }
        if (borrowerApprovalVO.getIsCarOk()) {
            userIntegral = new UserIntegral();
            userIntegral.setUserId(borrower.getUserId());
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            userIntegral.setContent("借款人车辆信息");
            userIntegralService.save(userIntegral);
            integral+=IntegralEnum.BORROWER_CAR.getIntegral();
        }


        userInfo.setIntegral(integral);
        //设置审核状态
        userInfo.setBorrowAuthStatus(borrowerApprovalVO.getStatus());
        userInfoService.updateById(userInfo);
    }
}
